﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Drawing;
using TradeForm.config;
using System.Xml.Linq;
using System.Configuration;

namespace TradeForm
{
    public class DocumentBase : PrintDocument
    {
        protected string _defCode = string.Empty;

        protected BillExchange billex = new BillExchange();
        protected ProFormaInvoice pinv = new ProFormaInvoice();
        protected List<ProFormaInvoiceGoods> pinvGoodsList = new List<ProFormaInvoiceGoods>();
        protected Invoice inv = new Invoice();
        protected Contract cont = new Contract();
        protected CustomsDeclaration cust = new CustomsDeclaration();
        protected ConsignBill consb = new ConsignBill();
        protected PackList pack = new PackList();
        protected GoodsSummery goodsSum = new GoodsSummery();
        protected List<GoodsDetail> goodsDetails = new List<GoodsDetail>();
        protected List<PackListGoods> packGoodsList = new List<PackListGoods>();
        protected CompanyInfo _compny = new CompanyInfo();

        public BillExchange BillExchange
        {
            get { return billex; }
            set { billex = value; }
        }
        public Invoice Invoice
        {
            get { return inv; }
            set { inv = value; }
        }
        public Contract Contract
        {
            get { return cont; }
            set { cont = value; }
        }
        public CustomsDeclaration CustomsDeclaration
        {
            get { return cust; }
            set { cust = value; }
        }
        public PackList PackList
        {
            get { return pack; }
            set { pack = value; }
        }
        public GoodsSummery GoodsSummery
        {
            get { return goodsSum; }
            set { goodsSum = value; }
        }
        public List<GoodsDetail>  GoodsDetail
        {
            get { return goodsDetails; }
            set { goodsDetails = value; }
        }
        public List<PackListGoods>  PackListGoods
        {
            get { return packGoodsList; }
            set { packGoodsList = value; }
        }
        public CompanyInfo CompanyInfo
        {
            get { return _compny; }
            set { _compny = value; }
        }
        //protected Dictionary<string, Setting> dicPrintSets = new Dictionary<string, Setting>();

        protected Font Font = new Font("宋体", 10, GraphicsUnit.Point);
        protected Font TitleFont = new Font("宋体", 14, GraphicsUnit.Point);
        private bool _isPrintBack = false;
        public bool IsPrintBack
        {
            get { return this._isPrintBack; }
            set { this._isPrintBack = value; }
        }

        //public DialogResult ShowPrintPreviewDialog()
        //{
        //    PrintPreviewDialog dialog = new PrintPreviewDialog();
        //    //dialog.Bounds = new Rectangle(new Point(0, 0), new Size(400, 297));
        //    dialog.Document = this;
        //    return dialog.ShowDialog();
        //}

        public DialogResult ShowPageSettingsDiaglog()
        {
            PageSetupDialog dialog = new PageSetupDialog();
            dialog.Document = this;
            return dialog.ShowDialog();
        }



        protected void getData(string defcode)
        {
            DataDBDataContext dbcntxt = new DataDBDataContext
                    (ConfigurationManager.ConnectionStrings["TradeForm.Properties.Settings.dbConnectionString"].ConnectionString);


            #region //BillExchange
            var billexs = from bill in dbcntxt.BillExchange
                          where bill.DefCode == defcode
                          select bill;
            if (billexs.Count() > 0)
            {
                billex = billexs.First();
            }
            #endregion

            #region //ProFormaInvoice
            var pinvs = from bill in dbcntxt.ProFormaInvoice
                        where bill.DefCode == defcode
                        select bill;
            if (pinvs.Count() > 0)
            {
                pinv = pinvs.First();
            }
            #endregion

            #region //ProFormaInvoiceGoods
            var pinvGoodss = from bill in dbcntxt.ProFormaInvoiceGoods
                             where bill.DefCode == defcode
                             select bill;
            if (pinvGoodss.Count() > 0)
            {
                this.pinvGoodsList = pinvGoodss.ToList();
            }
            #endregion

            #region //Invoice
            var invs = from bill in dbcntxt.Invoice
                       where bill.DefCode == defcode
                       select bill;
            if (invs.Count() > 0)
            {
                inv = invs.First();
            }
            #endregion

            #region //Contract
            var conts = from bill in dbcntxt.Contract
                        where bill.DefCode == defcode
                        select bill;
            if (conts.Count() > 0)
            {
                cont = conts.First();
            }
            #endregion

            #region //CustomsDeclaration
            var custs = from bill in dbcntxt.CustomsDeclaration
                        where bill.DefCode == defcode
                        select bill;
            if (custs.Count() > 0)
            {
                cust = custs.First();
            }
            #endregion

            #region //ConsignBill
            var consbs = from bill in dbcntxt.ConsignBill
                         where bill.DefCode == defcode
                         select bill;
            if (consbs.Count() > 0)
            {
                consb = consbs.First();
            }
            #endregion

            #region //PackList
            var packs = from bill in dbcntxt.PackList
                        where bill.DefCode == defcode
                        select bill;
            if (packs.Count() > 0)
            {
                pack = packs.First();
            }
            #endregion

            #region //PackListGoods
            var packgoods = from bill in dbcntxt.PackListGoods
                            where bill.DefCode == defcode
                            select bill;
            if (packgoods.Count() > 0)
            {
                this.packGoodsList = packgoods.ToList();
            }
            #endregion

            #region //GoodsSummery
            var goodsSums = from bill in dbcntxt.GoodsSummery
                            where bill.DefCode == defcode
                            select bill;
            if (goodsSums.Count() > 0)
            {
                goodsSum = goodsSums.First();
            }
            #endregion

            #region //GoodsDetail
            var goods = from good in dbcntxt.GoodsDetail
                        where good.DefCode == defcode
                        select good;
            if (goods.Count() > 0)
            {
                this.goodsDetails = goods.ToList();
            }
            #endregion

            var coms = from com in dbcntxt.CompanyInfo
                       select com;
            if (coms != null && coms.Count() > 0)
            {
                this._compny = coms.First();
            }
        }


    }
}
